﻿using Client.Framework.Message;
using Client.Model;
using Client.Util;
using Google.Protobuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.Message.Request
{
    class AddEditUserReq : RequestBaseMessage
    {
        public AddEditUserReq(int msgId) : base(msgId)
        {

        }

        public ViewUser User { get; set; }
        public List<int> RoleIds { get; private set; } = new List<int>();

        public override byte[] Serialize()
        {
            AddEditUser msg = new AddEditUser();
            if (User != null)
            {
                User user = new User
                {
                    Id = User.ID,
                    OrgId = User.OrgId,
                    LoginName = User.LoginName,
                    Password = MD5Util.MD5Encrypt(User.Password),
                    Status = (int)User.Status,
                    Sort = User.Sort,
                };
                RoleIds.ForEach(roleId =>
                {
                    user.RoleIds.Add(roleId);
                });
                msg.User = user;
            }
            
            return msg.ToByteArray();
        }
    }
}
